package com.vhall.component.plugin.common.utils;

//import jodd.util.concurrent.ThreadFactoryBuilder;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ScheduledExecutorService;

/**
 * @author yuanzh
 */
@Slf4j
public class ThreadPoolUtil {

    private static final int CORE_POOL_SIZE = 20;

    private static final int POOL_MAX_SIZE = 200;

    private static final long KEEP_ALIVE_TIME = 30;

    private static final int BLOCK_QUEUE_SIZE = 100;

    private static ScheduledExecutorService scheduledExecutorService = null;

    private static final int SCHE_THREAD_SIZE = 5;

    /**
     * 生成线程池(队列满后拒绝并输出日志)
     *
     * @param corePoolSize   核心线程数
     * @param poolMaxSize    最大线程数
     * @param keepAliveTime  超时时间，单位：毫秒
     * @param blockQueueSize 队列大小
     * @param threadPoolName 线程池名称
     * @return
     */
//	public static ThreadPoolExecutor createThreadPoolExecutor(int corePoolSize, int poolMaxSize, long keepAliveTime,
//			int blockQueueSize, final String threadPoolName) {
//		log.info("createThreadPoolExecutor execute, threadPoolName=" + threadPoolName);
//		// init thread pool
//		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(blockQueueSize);
//		RejectedExecutionHandler handler = (r, executor) -> log
//				.error("Thread pool block queue is full! runnable=" + r + ", threadPoolName=" + threadPoolName);
//
//		ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadPoolName + "-%d").get();
//
//		return new ThreadPoolExecutor(corePoolSize, poolMaxSize, keepAliveTime, TimeUnit.MILLISECONDS, workQueue, threadFactory,
//									  handler);
//	}
//	/**
//	 * 生成默认线程池(队列满后拒绝并输出日志)
//	 *
//	 * @param threadPoolName 线程池名称
//	 * @return
//	 */
//	public static ThreadPoolExecutor createDefaultThreadPoolExecutor(final String threadPoolName ) {
//		log.info("createThreadPoolExecutor execute, threadPoolName=" + threadPoolName);
//		// init thread pool
//		BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(BLOCK_QUEUE_SIZE);
//		RejectedExecutionHandler handler = (r, executor) -> log
//				.error("Thread pool block queue is full! runnable=" + r + ", threadPoolName=" + threadPoolName);
//
//		ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadPoolName + "-%d").get();
//
//		return new ThreadPoolExecutor(CORE_POOL_SIZE, POOL_MAX_SIZE, KEEP_ALIVE_TIME, TimeUnit.MILLISECONDS, workQueue, threadFactory,
//									  handler);
//	}
//
//
//	public static ScheduledExecutorService getScheduledExecutorService(String threadPoolName) {
//
//		if (!isScheduledServiceEnable()) {
//			ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(threadPoolName + "-%d").get();
//			scheduledExecutorService = new ScheduledThreadPoolExecutor(SCHE_THREAD_SIZE,threadFactory);
//		}
//
//		return scheduledExecutorService;
//	}
//
//
//	public static boolean isScheduledServiceEnable() {
//		return !(scheduledExecutorService == null
//				|| scheduledExecutorService.isShutdown()
//				|| scheduledExecutorService.isTerminated());
//	}

}
